package javaNIO1.regularExpressions;

import java.util.LinkedList;
import java.util.regex.Pattern;

/**
 * Created with Intel IDEA.
 *
 * @Auther: gjt
 * @Date: 2024/7/28 15:18
 */
public class Poodle {
    public static void main(String[] args) {
        String input = "poodle zoo";
        Pattern space = Pattern.compile(" ");
        Pattern d = Pattern.compile("d");
        Pattern o = Pattern.compile("o");
        Pattern[] patterns = {space, d, o};
        int limits[] = {1, 2, 5, -2, 0};
        generate(input, patterns, limits);

    }

    private static void generate(String input, Pattern[] patterns, int[] limits) {
        System.out.println("<?xml version='1.0'?>");
        System.out.println("<table>");
        System.out.println("\t<row>");
        System.out.println("\t\r<head>Input: " + input + "</head>");
        for (int i = 0; i < patterns.length; i++) {
            Pattern pattern = patterns[i];
            System.out.println("\t\t<head>Regex: <value>" + pattern.pattern() + "</value></head>");
        }
        System.out.println("\t</row>");

        for (int i = 0; i < limits.length; i++) {
            int limit = limits[i];
            System.out.println("\t<row>");
            System.out.println("\t\t<entry>Limit: " + limit + "</entry>");
            for (int j = 0; j < patterns.length; j++) {
                Pattern pattern = patterns[j];
                String[] tokens = pattern.split(input, limit);
                System.out.println("\t\t<entry>");
                for (int k = 0; k < tokens.length; k++) {
                    System.out.println("<value>" + tokens[k] + "</value>");
                }
                System.out.println("\t\t</entry>");
            }
            System.out.println("\t</row>");
        }
        System.out.println("</table>");
    }

    private static Pattern[] getPatterns(String[] args) {
        LinkedList linkedList = new LinkedList();
        for (int i = 0; i < args.length; i++) {
            linkedList.add(Pattern.compile(args[i]));
        }
       Pattern[] patterns = new Pattern[linkedList.size()];
        return (Pattern[]) linkedList.toArray(patterns);
    }
}
